package com.tbit.uqbike.client.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tbit.uqbike.client.dao.core.UserDao;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.service.RedisService;
import com.tbit.uqbike.client.service.XycxBorrowCheckService;
import com.tbit.uqbike.client.util.DateTimeUtil;
import com.tbit.uqbike.client.util.HttpClientUtils;
import com.tbit.uqbike.client.util.LogUtil;
import com.tbit.uqbike.client.util.MD5PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: chen
 * @Description: 小雨出行武汉大学借车校验
 * @Date: 创建于 11:48 2021/10/22
 */
@Service
public class XycxBorrowCheckServiceImpl implements XycxBorrowCheckService {

    /**
     * 获取token接口
     */
    String getTokenUrl = "https://nicsvc001.whu.edu.cn/data_api/get_token/open_api/authentication/get_access_token";
    /**
     * 借车校验接口
     */
    String checkUrl = "https://nicsvc001.whu.edu.cn/data_api/get_data/open_api/customization/tunknownvapisfxnss/full";

    @Autowired
    private RedisService redisService;
    @Autowired
    private UserDao userDao;

    @Override
    public void checkBorrow(String idNO, Integer userId) {
        idNO = MD5PasswordUtil.MD5(idNO);
        Boolean result = check(idNO);
        if (!result) {
            throw new BaseException("-30009", "车辆只限本校师生使用");
        }
        User user = new User();
        user.setStudentAuth(true);
        user.setUserId(userId);
        user.setUpdateTime(DateTimeUtil.getNowTime());
        userDao.updateStudentAuth(user);
    }

    /**
     * 获取token
     *
     * @return
     */
    public String getToken() {
        String token = redisService.get("borrow.token");
        if (token == null) {
            String url = getTokenUrl + "?key=20211020206014507238746091765765160&secret=2d63810bc5aaeb43eda2e3e1fbcfa104b0c46fee";
            String json = HttpClientUtils.httpGet(url, null);
            System.out.println("获取token=" + json);
            if (json != null) {
                JSONObject jsonObject = JSON.parseObject(json);
                if ("10000".equals(jsonObject.getString("code"))) {
                    token = jsonObject.getJSONObject("result").getString("access_token");
                    Integer expires_in = jsonObject.getJSONObject("result").getInteger("expires_in");
                    redisService.add("borrow.token", token, expires_in / 2);
                    return token;
                }
            }
        } else {
            return token;
        }
        return null;
    }

    /**
     * 校验身份
     *
     * @param idNO
     * @return
     */
    public Boolean check(String idNO) {
        Boolean isTrue = false;
        String token = getToken();
        String json = HttpClientUtils.httpGet(checkUrl + "?access_token=" + token + "&SFZJH=" + idNO, null);
        LogUtil.info("身份校验=" + json);
        if (json != null) {
            JSONObject jsonObject = JSON.parseObject(json);
            if ("ok".equals(jsonObject.getString("message"))) {
                String result = jsonObject.getString("result");
                if (result != null) {
                    String isStudent = JSON.parseObject(result).getJSONArray("data").getJSONObject(0).getString("SFXNSS");
                    if ("1".equals(isStudent)) {
                        isTrue = true;
                    }
                }
            }
        } else {
            isTrue = true;
        }
        return isTrue;
    }


}
